﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.Car_Rental;
using Data.Car_Rental;
using System.Text;

namespace Background.Controllers
{
    public class AreaController : Common.Background.Web.Controllers.BackgroundBaseController
    {
        AreaRepository iAreaServices = null;
        public AreaController()
        {
            iAreaServices = new AreaRepository();
        }

        public ActionResult Index(string name, int? page)
        {
            VP = new Entity.VPredication();
            GetCategory();
            if (!string.IsNullOrEmpty(name))
                VP.AddItem("Name", name);
            PP = new Entity.PagingParam(page ?? 1, base.PageSize);
            var model = iAreaServices.GetCommon_Area(VP, PP);
            if (Request.IsAjaxRequest())
                return PartialView("List", model);

            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            Area entity = null;
            string names = form["Name"];
            names = names.Replace("，", ",");
            if (string.IsNullOrEmpty(names))
                ModelState.AddModelError("Name", "请填写区域名称");
            else
            {
                names.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(i =>
                {
                    entity = new Area();
                    TryUpdateModel(entity);
                    entity.Name = i;
                    iAreaServices.AddCommon_Area(entity);
                });
                return AlertToUrl("操作成功", "Index");
            }
            return View();
        }


        public ActionResult Edit(int? codeID)
        {
            if (codeID.HasValue)
                return View(iAreaServices.GetCommon_Area(codeID.Value));
            else
                return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult Edit(FormCollection form)
        {
            int codeid;
            int.TryParse(form["codeID"], out codeid);
            Area entity = iAreaServices.GetCommon_Area(codeid);
            TryUpdateModel(entity);
            if (iAreaServices.EditCommon_Area(entity).IsComplete)
                return AlertToUrl("操作成功", "Index");
            return View();
        }

        public ActionResult Delete(string codeID)
        {
            List<int> arrID = new List<int>();
            codeID.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(i =>
            {
                arrID.Add(Convert.ToInt32(i));
            });
            arrID.ForEach(i =>
            {
                iAreaServices.DeleteCommon_Area(iAreaServices.GetCommon_Area(i));
            });
            return AlertToUrl("操作成功", "Index");
        }

        #region 区域树结构
        #region 分类树结构
        /// <summary>
        /// 得到分类
        /// </summary>
        public void GetCategory()
        {
            Area common_Area = iAreaServices.GetCommon_AreaTree();
            StringBuilder html = new StringBuilder();
            html.Append("<p><ul id='containerul'>");
            this.GetSubCategory(html, common_Area, 0);
            html.Append("</ul></p>");
            ViewData["CategoryData"] = html.ToString();
        }

        /// <summary>
        /// 通过递归拼树形结构
        /// </summary>
        /// <param name="html">树形Html字符串</param>
        /// <param name="category">树形商品分类对象</param>
        private void GetSubCategory(StringBuilder html, Area common_Area, int num)
        {
            num++;
            html.Append("<li id='" + common_Area.CodeID + "' name='" + num.ToString() + "'>");
            html.Append("<a href='javascript:void(0)' id='" + common_Area.Name + "'value='" + common_Area.CodeID + "'>");
            html.Append("<ins>&nbsp;</ins>");
            html.Append(common_Area.CodeID + "-" + common_Area.Name);
            html.Append("</a>");
            if (common_Area.Sons != null && common_Area.Sons.Count() > 0)
            {
                html.Append("<ul>");
                foreach (var item in common_Area.Sons)
                {
                    this.GetSubCategory(html, item, num);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }
        #endregion

        #endregion
    }
}
